package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;

import java.awt.event.*;
import java.util.Vector;
public class Xueyiwugong extends JPanel implements MouseListener{

    DuobaoSwingLaunch game;
    Map heroMap;
    Vector vector;//用于分页
    int newPage;//当前页数
    JLabel jlb_nextPage;//下一页
    JLabel jlb_previousPage;//上一页
    int vector_previous_size;
    NPC npc;


    JLabel describe[];
    JLabel wugong[];


    JLabel fanhui;
    JLabel fanhuiyouxi;
    public Xueyiwugong(DuobaoSwingLaunch game, NPC npc, int page)
    {
        vector=new Vector();
        this.newPage=page;
        this.game=game;
        this.npc=npc;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);


        setDescribe();
        setWugong();

        setFanhui();
        setFanhuiyouxi();
        setadd();
    }



    public void setDescribe() {
        String Des[]=SplitString.splitString("请选择要学习的武功，武功名称后括号中的数表示该项武功的等级");
        int leng=Des.length;
        describe=new JLabel[leng];

        for(int i=0;i<leng;i++)
        {
            describe[i]=new JLabel();
            describe[i].addMouseListener(this);
            describe[i].setFont(myFont.f2);
            describe[i].setText(Des[i]);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(describe[i]);
            this.vector.add(jp1);
        }
    }
    public void setWugong() {
        int a = this.npc.getWGs().size();
        wugong = new JLabel[a];
        for(int i=0;i<a;i++)
        {
            WuGong wogong =  this.npc.getWGs().get(i);
            wugong[i] = new JLabel();

            if(wogong.isPZ())
            {
                wugong[i].setText("<html><u>"+"□"+wogong.getName()+"("+wogong.getLv()+"级)"+"</u></html>");
            }else
            {
                wugong[i].setText("<html><u>"+wogong.getName()+"("+wogong.getLv()+"级)"+"</u></html>");
            }


            wugong[i].setFont(myFont.f2);
            wugong[i].addMouseListener(this);
            wugong[i].setForeground(Color.BLUE);

            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(wugong[i]);
            this.vector.add(jp1);


        }
    }


    public void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.vector.add(jp3);
    }

    public void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.vector.add(jp4);
    }

    private void setadd()//加载JLabel
    {
        JPanel jPanel=new JPanel();
        jPanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        if(this.vector.size()<=this.newPage*15)
        {
            if(this.newPage!=1)
            {
                jlb_previousPage=new JLabel();
                jlb_previousPage.addMouseListener(this);
                jlb_previousPage.setFont(myFont.f2);
                jlb_previousPage.setForeground(Color.BLUE);
                jlb_previousPage.setText("<html><u>"+"上一页"+"</u></html>");
                jPanel.add(jlb_previousPage);
                this.vector.add(this.vector.size()-2,jPanel);
            }
            for(int i=((this.newPage-1)*15);i<this.vector.size();i++)
            {
                this.add((Component) this.vector.get(i));
            }
        }else
        {
            for(int i=((this.newPage-1)*15);i<(this.newPage)*15;i++)
            {
                this.add((Component) this.vector.get(i));
            }

            if(this.newPage!=1)
            {
                jlb_previousPage=new JLabel();
                jlb_previousPage.addMouseListener(this);
                jlb_previousPage.setFont(myFont.f2);
                jlb_previousPage.setForeground(Color.BLUE);
                jlb_previousPage.setText("<html><u>"+"上一页"+"</u></html>");
                jPanel.add(jlb_previousPage);
            }
            jlb_nextPage=new JLabel();
            jlb_nextPage.addMouseListener(this);
            jlb_nextPage.setFont(myFont.f2);
            jlb_nextPage.setForeground(Color.BLUE);
            jlb_nextPage.setText("<html><u>"+"下一页"+"</u></html>");
            jPanel.add(jlb_nextPage);
            this.add(jPanel);
        }
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        for(int i=0;i<describe.length;i++)
        {
            if(jl==describe[i])
            {
                f=true;
            }
        }
        return f;
    }
    private int iFzhuangbei(JLabel jl)
    {
        int id=-1;
        int leng=this.wugong.length;
        for(int i=0;i<leng;i++)
        {
            if(jl==this.wugong[i])
            {
                id=i;

            }
        }
        return id;
    }
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        int id=this.iFzhuangbei((JLabel)e.getSource());
        if(id!=-1)
        {
            WuGong wogong = this.npc.getWGs().get(id);
            this.game.setPanel(new Xueyi(this.game,this.npc,wogong));

        }else if(e.getSource()==jlb_nextPage)
        {
            this.game.setPanel(new Xueyiwugong(this.game,this.npc,(this.newPage+1)));
        }else if(e.getSource()==jlb_previousPage)
        {
            this.game.setPanel(new Xueyiwugong(this.game,this.npc,(this.newPage-1)));
        }else if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            this.game.setPanel(new NPCXinxi(this.game,this.npc,1));
        }
    }

    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }

    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}
